﻿<script type="text/javascript">
    var isFirstLoad = true;
    var isFirstLoad1 = true;
    var target;

    $(document).ready(function () {
        $("#tabs").tabs();
        $("#MenuNews").removeClass("dropdown").addClass("active");
        CKEDITOR.replace('DetailsVn', { htmlEncodeOutput: true, width: "100%" });
        CKEDITOR.replace('DetailsEn', { htmlEncodeOutput: true, width: "100%" });
        $("#dialog").dialog({ autoOpen: false, width: 400, height: 100 });
        $("#dialog1").dialog({ autoOpen: false, width: 400, height: 100 });
        NewsValidate();
    });

    function InsertImage() {
        if (CKEDITOR.currentInstance) {
            target = CKEDITOR.currentInstance.name;
            $("#dialog").dialog("open");
        }
        else
            alert('Vui lòng chọn văn bản');
    }

    function SubmitUploadImage() {
        var image = '<img alt="loading..." width="24px" height="24px" src="@Url.Content("~/Areas/Admin/Content/Images/loading.gif")" />';
        $("#loading").append(image);
        $("#ImgForm").submit();
    }
    function UploadImage_Complete() {
        if (isFirstLoad == true) {
            isFirstLoad = false;
            return;
        }
        document.getElementById("ImgForm").reset();
        var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);
        if (newImg.IsValid == false) {
            alert(newImg.Message);
            $("#loading").empty();
            return;
        }
        var imgHtml = CKEDITOR.dom.element.createFromHtml("<img src='" + newImg.ImagePath + "' alt='image'/>");
        if (target == "DetailsVn") {
            CKEDITOR.instances['DetailsVn'].insertElement(imgHtml);
        }
        if (target == "DetailsEn") {
            CKEDITOR.instances['DetailsEn'].insertElement(imgHtml);
        }
        $("#loading").empty();
        $("#dialog").dialog("close");
    }

    function NewsValidate() {
        $("#NewsForm").validate({
            rules: {
                TitleVn: { required: true }
            },
            messages: {
                TitleVn: { required: "Nhập tiêu đề tiếng việt cho bài viết" }
            }
        });
    }

    function InsertImage1() {
        $("#dialog1").dialog("open");
    }

    function SubmitUploadImage1() {
        var image = '<img alt="loading..." width="24px" height="24px" src="@Url.Content("~/Areas/Admin/Content/Images/loading.gif")" />';
        $("#loading1").append(image);
        $("#ImgForm1").submit();
    }

    function UploadImage_Complete1() {
        if (isFirstLoad1 == true) {
            isFirstLoad1 = false;
            return;
        }
        document.getElementById("ImgForm1").reset();
        var newImg = $.parseJSON($("#UploadTarget1").contents().find("#jsonResult")[0].innerHTML);
        if (newImg.IsValid == false) {
            alert(newImg.Message);
            $("#loading1").empty();
            return;
        }
        $("#NewsImageView").attr("src", newImg.ImagePath.replace('images/', 'images/thumb/'));
        $("#NewsImage").val(newImg.ImagePath);
        $("#loading1").empty();
        $("#dialog1").dialog("close");
    }
</script>
@{
    ViewBag.Title = "Dat Nuoc Viet";
}
<div class="contenttitle">
    <div class="titleplace">
        <span>Thêm bài viết</span>&nbsp;&nbsp;&nbsp;
        @if (ViewBag.message != null)
        {
            <span style="font-weight:600;color:Red;">@ViewBag.message</span>
        }
    </div>
    <div class="buttonplace">
        <a href="@Url.Action("News", "AdminNews")">
            <input type="button" value="Danh sách bài viết" />
        </a>
    </div>
    <div class="clear">&nbsp;</div>
</div>
<div class="contentitem">
    @using (Html.BeginForm("CreateNews", "AdminNews", FormMethod.Post, new { id = "NewsForm", enctype = "multipart/form-data" }))
    {
        <p style="font-weight:bold;">Thông tin bài viết</p>
        <div id="tabs">
            <ul>
                <li><a href="#tabs-1">Trang tiếng việt</a></li>
                <li><a href="#tabs-2">Trang tiếng anh</a></li>
            </ul>
            <div id="tabs-1">
                <div class="atributelabel">Tên bài viết <span style="color:Red;">(*)</span></div>
                <div class="atributeeditor"><input type="text" class="input250" name="TitleVn" id="TitleVn"/></div>
                <div class="clear">&nbsp;</div>
                <textarea style="width:100%;" name="DescriptionVn" id="DescriptionVn" rows="1" cols="1"></textarea>
                <br /><br />
                <textarea name="DetailsVn" id="DetailsVn" rows="1" cols="1">
                </textarea>
            </div>
            <div id="tabs-2">
                <div class="atributelabel">Tên bài viết</div>
                <div class="atributeeditor"><input type="text" class="input250" name="TitleEn" id="TitleEn"/></div>
                <div class="clear">&nbsp;</div>
                <textarea style="width:100%;" name="DescriptionEn" id="DescriptionEn" rows="1" cols="1"></textarea>
                <br /><br />
                <textarea name="DetailsEn" id="DetailsEn" rows="1" cols="1">
                </textarea>
            </div>
        </div>
        <br />
        <div class="atributelabel">Loại tin</div>
        <div class="atributeeditor">
            <select name="NewsType" id="NewsType">
                @foreach (var item in (List<DatNuocViet.Models.GetNewsTypeResult>)ViewBag.NewsType)
                {
                    <option value="@item.Id">@item.NewsTypeVn</option>
                }
            </select>
        </div>
        <div class="clear">&nbsp;</div>
        <div class="atributelabel" style="padding-top:50px;">Ảnh đại diện</div>
        <div class="atributeeditor">
            <img id="NewsImageView" alt="image" width="110px" height="83px" src="@Url.Content("/Resource/no_image.gif")" /><br />
            <input type="text" class="input250" name="NewsImage" id="NewsImage" readonly value="~/Resource/no_image.gif"/><input type="button" value="Chọn hình ảnh" onclick="InsertImage1();"/>
        </div>
        <div class="clear">&nbsp;</div>
        <div class="atributelabel">&nbsp;</div>
        <div class="atributeeditor">
            <input type="button" value="Chèn hình ảnh" onclick="InsertImage();"/>
            <input type="submit" value="Lưu bài viết"/>
        </div>
        <div class="clear">&nbsp;</div>
    }
</div>
<div id="dialog" title="Upload hình ảnh">
     @using (Html.BeginForm("UploadImage", "AdminHome", FormMethod.Post, new { enctype = "multipart/form-data", id = "ImgForm", name = "ImgForm", target = "UploadTarget" }))
     {
        <input type="hidden" name="folderName" id="folderName" value="news/upload" />
        <input type="file" name="UploadImage" id="UploadImage"/>
        <input type="button" value="Upload" onclick="SubmitUploadImage();" />
        <span id="loading"></span>
        <br />
        <small style="color:Red;">* Hình ảnh nhỏ hơn 1mb - Định dạng file jpg, gif, png</small>
     }
    <iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute;left: -999em; top: -999em;"></iframe>    
</div>
<div id="dialog1" title="Upload hình ảnh">
    @using (Html.BeginForm("UploadImageWidthSave1", "AdminHome", FormMethod.Post, new { enctype = "multipart/form-data", id = "ImgForm1", name = "ImgForm1", target = "UploadTarget1" }))
    {
        <input type="hidden" name="folderName1" id="folderName1" value="news/images" />
        <input type="file" name="UploadImage1" id="UploadImage1" />
        <input type="button" value="Upload" onclick="SubmitUploadImage1();" />
        <span id="loading1"></span>
        <br />
        <small style="color: Red;">* Hình ảnh nhỏ hơn 1mb - Định dạng file jpg, gif, png</small>
        <iframe id="UploadTarget1" name="UploadTarget1" onload="UploadImage_Complete1();" style="position: absolute;left: -999em; top: -999em;"></iframe>
    }
</div>